<?php
namespace app\erp\model;

use think\Model;
use think\Cache;

class Expressdb extends Model
{

    public $param = array();

    public $lastId;

    protected $pk = 'express_id';

    protected $name = 'express';

    protected $type = [
        'create_time' => 'timestamp:Y-m-d H:i',
        'receipt_time' => 'timestamp:Y-m-d H:i'
    ];
    // 自动完成时间转换
    protected $autoWriteTimestamp = true;
    // 自动完成
    protected $insert = [
        'partner_id' => PARTNER_ID,
        'receipt_state' => 1,
        'site_id'
    ];

    protected function setSiteIdAttr($value)
    {
        if (! $value) {
            return SITE_ID;
        }
        if ($value == 0) {
            return null;
        }
        return $value;
    }

    protected function cache_name($name)
    {
        $new_arr = $this->param;
        $new_arr['partner_id'] = PARTNER_ID;
        if (! isset($new_arr['site_id'])) {
            $new_arr['site_id'] = SITE_ID;
        }
        ksort($new_arr);
        $param_str = http_build_query($new_arr);
        return md5($name . $param_str);
    }

    public function scopeOrderNo($query, $order_no)
    {
        $query->where('order_no', $order_no);
    }

    public function _emptyParam()
    {
        $this->param = array();
    }

    public function scopeExpressId($query, $express_id)
    {
        $query->where('express_id', $express_id);
    }
    // 基础查询
    public function base($query)
    {
        //$query->where('partner_id', PARTNER_ID);
        if (isset($this->param['order_no']) && $this->param['order_no'] != '') {
            $query->orderNo($this->param['order_no']);
        }
        if (isset($this->param['express_id'])) {
            $query->expressId($this->param['express_id']);
        }
    }

    public function ajaxFind()
    {
        $data = $this->master()->find();
        if ($data) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    public function ajaxSave()
    {
        if (isset($this->param['express_id']) && $this->param['express_id'] != '') {
            $this->allowField(true)->save($this->param, [
                'express_id' => $this->param['express_id']
            ]);
        } else {
            $this->allowField(true)->save($this->param);
        }
        $this->lastId = $this->express_id;
        Cache::clear('express');
        return 'success';
    }

    public function ajaxSaveField()
    {
        if (isset($this->param['express_id']) && $this->param['express_id'] != '') {
            $data[$this->param['field']] = $this->param['value'];
            $this->save($data, [
                'express_id' => $this->param['express_id']
            ]);
        }
        Cache::clear('express');
        return 'success';
    }

    public function ajaxList()
    {
        $data = $this->order('express_id asc')
            ->cache($this->cache_name('express_list'), 86400, 'express')
            ->select();
        if (! empty($data)) {
            $data = $data->toArray();
            foreach ($data as $key => $val) {
                $data[$key] = $this->handleData($val);
            }
        }
        return $data;
    }

    protected function handleData($val)
    {
        $uploaddb = new Uploaddb();
        foreach ($val as $k => $v) {
            $val[$k] = $v;
            switch ($k) {
                case 'cart_ids':
                    $cartdb = new Cartdb();
                    $cartdb->param['order_no'] = $val['order_no'];
                    $cartdb->param['cart_id'] = explode(',', $v);
                    $cartdb->param['need_goods_info'] = 'no';
                    $cartdb->param['need_goods_title'] = 'yes';
                    $cartdb->param['site_id'] = - 1;
                    $val['cart_list'] = $cartdb->ajaxList()['list'];
                    break;
                case 'express':
                    $val['express_name'] = changeExpress($v);
                    break;
                case 'express_proof':
                    if ($val['express_proof']) {
                        $val['express_proof'] = $uploaddb->getFind($v);
                    }
                    break;
            }
        }
        return $val;
    }
}